<?php
namespace Admin\Controller;
use Think\Controller;
class NewsController extends AdminController {
    public function index(){
        $News = M('News');//实例化AdminUser对象
        $count = $News->count();// 查询满足要求的总记录数
        $itemNum = 6; //每页显示的记录数
        $show = c_page($count,'条新闻');
        //page方法分页
        $data = $News
                ->order('u_time desc, id desc')
                ->page(I('get.p',1) . ',' . $itemNum)
                ->select();
//        var_dump($data);
//        exit;

        foreach ($data as $k => $v){
            $data[$k]['title'] = subtext(strip_tags($v['title']),20,'...');
        }


        if(IS_AJAX){
            $json['data'] = $data;
            $json['page'] = $show;
            $this->ajaxReturn($json);
        }else{
            $this->assign('title','新闻列表');//标题
            $this->assign('data',$data);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
        }
    }


    /**
     * 添加
     */
    public function add(){
        if(IS_POST){
            //整合数据
            $data = I('post.');
            $data['content'] = I('post.content','','clear_js');
            $data['more_img'] = I('post.more_img');
            $data['more_img'] = implode(',', $data['more_img']);

            //

            $News = D('News');
            $News->setData($data);
            //记得报错
            $res = $News->insertDb();

            if($res){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败 '.$News->getError());
            }
            exit;
        }else{
            $this->assign('title','新闻添加');//标题
            $this->display();
        }
    }




    //$id 表示URL问号后面的id字段
    public function edit($id = 0){
        if($id < 1){
            $this->error('参数错误');
        }
        if(IS_POST){
            $data = I('post.');
            $data['content'] = I('post.content','','clear_js');
            $Goods = D('News');
            $Goods->setData($data);
            //记得报错
            $res = $Goods->updateDb();
            if($res !== false){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('修改失败 '.$Goods->getError());
            }
        }else{
            $one = M('News')->find($id);
            $this->assign('title','新闻修改');//标题
            $this->assign('one',$one);//该ID的新闻
            $this->display();
        }

    }

    public function center(){
        if(IS_POST){
            $data = I('post.');
            $data['info'] = I('post.info','','clear_js');
            $data['c_time']=$data['u_time'] = time();
            $res = M('NewsCenter')->data($data)->add();
            if($res){
                $this->success('设置成功',U('center'));
            }else{
                $this->error('设置失败');
            }
        }else{
            $one = M('NewsCenter')->order('u_time desc')->find();
            $this->assign('one',$one);
            $this->assign('title','新闻中心');
            $this->display();
        }
    }

    public function delete($id){
        if($id<1){
            $this->error('参数错误,至少选择一条数据');
        }
        if(!is_array($id)){
            $res = M('News')->delete($id);
        }else{
            $wh['id'] = array('in',implode(',', $id));
            $res = M('News')->where($wh)->delete();
        }
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败：',U('index'));
        }
    }



    public function upload_img($type=0)
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2 * 1024 * 1024 ;// 设置附件上传大小 2m
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // $upload->savePath = 'Goods/'; // 设置附件上传（子）目录
        $upload->subName = array('date','Y/m/d');
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
//        dump($info);
//        exit;
        if(!$info) { // 上传错误提示错误信息
            $this->ajaxReturn(array("error"=>$upload->getError()));

        }else{ // 上传成功
            $pic_url = 'Uploads/'.$info['savepath'].$info['savename'];
            $image = new \Think\Image();
            $img = $image->open($pic_url);
            // $img1 = clone($img);
//            exit;




//          $thumb = get_thumb($pic_url);//_thumb

            /*  //缩小 图片
                //生成一个缩放后填充大小150*150的缩略图并保存为thumb.jpg
                $image->open($water['mark_img']);
                $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_FILLED)->save('./thumb.jpg');*/
            $water = M('Water')->find();
            $waterImgUrl = strstr($water['mark_img'],'Uploads');

            if(!$type){
                $img->thumb(243, 334,3)->water($waterImgUrl,$water['mark_pos'],$water['alpha'])->save($pic_url);
            }elseif($type == 1){
                $img->thumb(490, 490,3)->water($waterImgUrl,$water['mark_pos'],$water['alpha'])->save($pic_url);
            }elseif($type == 2){
                $img->thumb(647, 453,3)->save($pic_url);
            }
            // $img->thumb(400, 800,3)->water($water['mark_img'])->save($thumb);
            // $img1->water($water['mark_img'])->save($pic_url);
            $this->ajaxReturn(array('error'=>'0','pic'=>__ROOT__.'/'.$pic_url,'name'=>$info['savename']));
        }
    }
}
?>